package cc.controller;

import cc.face.WebFaceCompare;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * TODO
 *
 * @author My
 * @date 2021/6/24 13:21
 */
@RestController
@RequestMapping("/submit")
@Api(value = "测试上传接口", tags = "用于测试上传的接口", description = "测试上传接口")
public class UploadController {




    @PostMapping(value = "/upload.do", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "上传成功！"),
            @ApiResponse(code = 500, message = "上传失败！")
    })
    public Object upload(@ApiParam(value = "文件", required = true) MultipartFile file) throws Exception {//1. 接受上传的文件  @RequestParam("file") MultipartFile file
        //String imgUrl = this.uploadByOss(file);
        if (!file.isEmpty()) {
            if (file.getContentType().contains("image")) {
                try {
                    // 获取图片的文件名
                    String fileName = file.getOriginalFilename();

                    // 存储临时文件的目录
                    String savePath = "C:/temp";
                    File savePathDir = new File(savePath);
                    if(!savePathDir.exists()){
                        savePathDir.mkdirs();
                    }
                    File imgFile = new File(savePath + "/" + fileName);
                    file.transferTo(imgFile);


                    String who = WebFaceCompare.service(imgFile.getAbsolutePath());

                    //System.out.println(fileName);
                   return who;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
